// pages/mes/compony-id/compony-id.js
const defaultImg = 'https://hear2017.oss-cn-shanghai.aliyuncs.com/xianjian_wx_image/iv_license.png';
const uploadFile = require('../../../lib/aliyun/uploadAliyun.js');
const app = getApp();
import Toast from '../../../component/toast/toast';
import utils from '../../../utils/util.js';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        businessLicenseUrl: defaultImg
    },
    /**
     * ===================事件处理部分===================
     */
    chooseImg() {
        let that = this;
        wx.chooseImage({
            count: 1,
            sizeType: ['original'],
            sourceType: ['album', 'camera'],
            success(res) {
                // tempFilePath可以作为img标签的src属性显示图片
                that.setData({
                    businessLicenseUrl: res.tempFilePaths[0]
                });
            }
        })
    },
    clickSave(){
        let that = this;
        let imgPath = that.data.businessLicenseUrl;
        if (imgPath != defaultImg){
            wx.showLoading({
                title: '图片保存中...',
                mask: true
            });
            let dir = 'xianjian_wx_image';
            // console.log(imgPath);
            that.uploadSyncImg(imgPath, dir).then(res => {
                if (res.statusCode == 200) {
                    let imgUrl = res.imgUrl;
                    let pages = getCurrentPages();
                    let prevPage = pages[pages.length - 2];  //上一个页面
                    wx.navigateBack({
                        delta: 1,
                        success: function (res) {
                            prevPage.setData({
                                businessLicense: imgUrl
                            });
                        }
                    });
                }
                console.log("upload success：" ,res);
                wx.hideLoading();
            }, err => {
                wx.hideLoading();
                console.log("err:" ,err);
                Toast("图片上传失败！")
            });
        }else{
            Toast("请上传您公司的营业执照！");
        }
    },
    /**
     * ===================事件处理部分===================
     */


    /**
     * -------------------网络请求部分-------------------
     */
    uploadSyncImg(path, dir) {
        let promis = new Promise(function (resolve, reject) {
            uploadFile({
                filePath: path,
                dir: dir,
                success: function (res) {
                    resolve(res);
                },
                fail: function (res) {
                    reject(res);
                }
            });
        });
        return promis;
    },
    /**
     * -------------------网络请求部分-------------------
     */
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})